/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
(function ($) {
    $.fn.layer = function (options) {
        var defaults = {
            selectAll: '#selectAll',
            selectSubmit: '#selectSubmit',
            selectAction: '#selectAction',
            deleteUrl: '',
            actionUrl: '',
            actionParameter: function () {}
        }

        var table = this;
        var id = $(this).attr('id');
        var ops = $.extend(defaults, options);

        //处理多选单选
        $(ops.selectAll).click(function () {
            $(table).find("[name='id[]']").each(function () {
                if ($(this).prop("checked")) {
                    $(this).prop("checked", false);
                } else {
                    $(this).prop("checked", true);
                }
            })
        });


        $(table).ajaxForm({
            beforeSubmit: checkForm, // 此方法主要是提交前执行的方法，根据需要设置
            success: complete, // 这是提交后的方法
            dataType: 'json'
        });

        function checkForm() {
                if ($('.Validform_error').length != 0) {
                    return false;
                }
        }

        function complete(data) {
            if (data.status == 1) {
                if (data.url == null || data.url == '') {
                    //不带连接
                    layer.alert(data.info, {icon: 1}, function () {
                        window.location.reload();
                    });
                } else {
                    //layer.msg(data.info, {icon: 1, shade: [0.8, '#393D49'], time: 1000}, function (index) {
                    //    layer.close(index);
                    //    window.location.href = data.url;
                    //});
                    layer.msg(data.info,{time:800}, function () {
                        window.location.href = data.url;
                    });
                }
            } else {
                //layer.alert(data.info, {icon: 2}, function () {
                //    window.location.reload();
                //});
                layer.msg(data.info);
                return false;
            }
        }

        //处理删除
        $(table).find('.js-del').click(function () {
            var obj = this;
            var div = $(obj).parent().parent();
            var url = $(obj).attr('url');
            if (url == '' || url == null || url == 'undefined') {
                url = ops.deleteUrl;
            }
            operat(
                    obj,
                    url,
                    function () {
                        div.remove();
                    },
                    function () {});
        });
        
        //处理批量提交
        $(ops.selectSubmit).click(function () {
            //记录获取
            var ids = new Array();
            $(table).find("[name='id[]']").each(function () {
                if ($(this).prop("checked")) {
                    ids.push($(this).val());
                }
            });
            toastr.options = {
                "positionClass": "toast-bottom-right"
            };

            if (ids.length == 0) {
                toastr.warning('请先选择操作记录');
                return false;
            }

            //操作项目
            var dialog = layer.confirm('你确认要进行本次批量操作！', function () {
                var parameter = $.extend({
                    ids: ids,
                    type: $(ops.selectAction).val()
                },
                        ops.actionParameter()
                        );
                $.post(ops.actionUrl, parameter, function (json) {
                    if (json.status) {
                        toastr.success(json.info);
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    } else {
                        toastr.warning(json.info);
                    }
                }, 'json');
                layer.close(dialog);
            });
        });


        //处理动作
        function operat(obj, url, success, failure) {
            var text = $(obj).attr('title');
            var dialog = layer.confirm('你确认执行' + text + '操作？', function () {
                var dload = layer.load('操作执行中，请稍候...');
                $.post(url, {
                    data: $(obj).attr('data')
                },
                        function (json) {
                            layer.close(dload);
                            layer.close(dialog);
                            if (json.status) {
                                toastr.success(json.info);
                                window.location.reload();
                                success();
                            } else {
                                toastr.warning(json.info);
                                failure();
                            }
                        }, 'json');
            });
        }

    };


    //时间插件
    $.fn.duxTime = function (options) {
        var defaults = {
            lang: 'ch'
        };
        var options = $.extend(defaults, options);
        this.each(function () {
            var id = this;
            $(id).datetimepicker(options);
        });
    };


})(jQuery);
